package com.miracle.film.controller;

import java.util.List;
import java.util.Optional;
import javax.servlet.http.HttpServletResponse;

import com.miracle.film.common.FilmConstant;
import com.miracle.film.domain.FlmFilmInfo;
import com.miracle.film.domain.FlmFilmListExt;
import com.miracle.film.service.IFlmFilmInfoService;
import com.miracle.film.service.IFlmFilmListService;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.miracle.film.domain.FlmFilmList;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 影单管理Controller
 * 
 * @author miracle
 * @date 2022-07-27
 */
@RestController
@RequestMapping("/film/filmList")
public class FlmFilmListController extends BaseController
{
    @Autowired
    private IFlmFilmListService flmFilmListService;
    @Autowired
    private IFlmFilmInfoService flmFilmInfoService;

    /**
     * 查询影单管理列表
     */
    @PreAuthorize("@ss.hasPermi('film:filmList:list')")
    @GetMapping("/list")
    public TableDataInfo list(FlmFilmList flmFilmList)
    {
        startPage();
        List<FlmFilmListExt> list = flmFilmListService.selectFlmFilmListList(flmFilmList);
        return getDataTable(list);
    }

    /**
     * 导出影单管理列表
     */
    @PreAuthorize("@ss.hasPermi('film:filmList:export')")
    @Log(title = "影单管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FlmFilmList flmFilmList)
    {
        List<FlmFilmListExt> list = flmFilmListService.selectFlmFilmListList(flmFilmList);
        ExcelUtil<FlmFilmListExt> util = new ExcelUtil<FlmFilmListExt>(FlmFilmListExt.class);
        util.exportExcel(response, list, "影单管理数据");
    }

    /**
     * 获取影单管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('film:filmList:query')")
    @GetMapping(value = { "/", "/{id}" })
    public AjaxResult getInfo(@PathVariable(value = "id", required = false) Integer id)
    {
        AjaxResult ajax = AjaxResult.success();
        FlmFilmInfo flmFilmInfo = new FlmFilmInfo();
        flmFilmInfo.setStatus(FilmConstant.FLM_STATUS.ENABLE);
        List<FlmFilmInfo> flmFilmInfos = flmFilmInfoService.selectFilm4Box(flmFilmInfo);
        ajax.put("films", flmFilmInfos);

        if ( StringUtils.isNotNull(id) ) {
            FlmFilmListExt flmFilmListExt = flmFilmListService.selectFlmFilmListById(id);
            ajax.put(AjaxResult.DATA_TAG, flmFilmListExt);
        }
        return ajax;
    }

    /**
     * 新增影单管理
     */
    @PreAuthorize("@ss.hasPermi('film:filmList:add')")
    @Log(title = "影单管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FlmFilmList flmFilmList)
    {
        return toAjax(flmFilmListService.insertFlmFilmList(flmFilmList));
    }

    /**
     * 修改影单管理
     */
    @PreAuthorize("@ss.hasPermi('film:filmList:edit')")
    @Log(title = "影单管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FlmFilmList flmFilmList)
    {
        return toAjax(flmFilmListService.updateFlmFilmList(flmFilmList));
    }

    /**
     * 删除影单管理
     */
    @PreAuthorize("@ss.hasPermi('film:filmList:remove')")
    @Log(title = "影单管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(flmFilmListService.deleteFlmFilmListByIds(ids));
    }
}
